﻿/*-----------------------------------------------
// File: DrawButton.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;
using Dict.Config;
using UI;


public class DrawButton : MonoBehaviour
{
    public GameObject MaskObj;
    public CommonIcon commonIcon;


    UIEventListener.VoidDelegate onDrawButtonClick;

    public UITweener clickMaskHide;
    public UITweener clickIconShow;

    public GameObject holdEffect;
    public GameObject clickEffect;

    public void Init(UIEventListener.VoidDelegate de)
    {
        onDrawButtonClick = de;
        MaskObj.SetActive(true);
        if (holdEffect != null)
            holdEffect.SetActive(false);
    }

    public void Holde() {
        if (holdEffect != null)
            holdEffect.SetActive(true);
    }

    public void OnDrawButtonClick()
    {

        _OnDrawButtonClick();
    }
    void _OnDrawButtonClick() {
        if (onDrawButtonClick != null)
        {
            onDrawButtonClick(gameObject);
        }
    }

    public void PlayTurnEffect()
    {
        if (holdEffect != null)
            holdEffect.SetActive(false);
        if (clickEffect != null)
            clickEffect.SetActive(true);
    }

    public void SetIcon(string reward, bool chosed, float delay) {
        StartCoroutine(_SetIcon(reward, chosed, delay));
    }

    IEnumerator _SetIcon(string reward, bool chosed, float delay) {
        yield return new WaitForSeconds(delay);
        SetIcon(reward, chosed);
    }

    public void SetIcon(string reward, bool chosed)
    {
        if (holdEffect != null)
            holdEffect.SetActive(false);
        clickMaskHide.ResetToBeginning();
        clickMaskHide.PlayForward();
        clickMaskHide.SetOnFinished(delegate {
            MaskObj.SetActive(false);
        });
        commonIcon.gameObject.SetActive(true);
        clickIconShow.ResetToBeginning();
        clickIconShow.PlayForward();
        char[] sp = { ',' };
        string[] spl = reward.Split(sp, System.StringSplitOptions.RemoveEmptyEntries);
        //string id = spl[0];
        //string num = spl[1];
        commonIcon.SetData(spl[0], false, int.Parse(spl[1]));
        if (!chosed) {
            commonIcon.SetDark();
        }
    }

    public void SetIcon(ProtoBuf.Message.BaseResource reward, bool chosed, float delay)
    {
        StartCoroutine(_SetIcon(reward, chosed, delay));
    }

    IEnumerator _SetIcon(ProtoBuf.Message.BaseResource reward, bool chosed, float delay)
    {
        yield return new WaitForSeconds(delay);
        SetIcon(reward, chosed);
    }

    public void SetIcon(ProtoBuf.Message.BaseResource reward, bool chosed) {
        if (holdEffect != null)
            holdEffect.SetActive(false);
        clickMaskHide.ResetToBeginning();
        clickMaskHide.PlayForward();
        clickMaskHide.SetOnFinished(delegate {
            MaskObj.SetActive(false);
        });
        commonIcon.gameObject.SetActive(true);
        clickIconShow.ResetToBeginning();
        clickIconShow.PlayForward();
        commonIcon.SetData(reward.id, false, reward.num);
        if (!chosed) {
            commonIcon.SetDark();
        }
    }
}